﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using AspNetCs.Model;
using AspNetCs.Bll;

public partial class Master_admin_Article : Admin_BasePage
{
    private string whereStr = "";
    AspNetCs.Bll.Article articleBLL = new AspNetCs.Bll.Article();
    DataTable data = new DataTable();
    protected void Page_Load(object sender, EventArgs e)
    {
        IsExit();
        if (!IsPostBack)
        {
            initPage();
        }
    }

    //绑定我的发布的文章
    protected void dataBindMyArticle()
    {
        Admin_Model adminModel = AdminInfo;
        if (adminModel==null)
        {
            Admin_LoginOut();
        }
        whereStr = " IsMaster=1 and EditorId={0}";
        whereStr = string.Format(whereStr, adminModel.Id);
        Pager p = new Pager();
        p.Obj = rptMyArticle;
        p.TableName = "Article";
        p.WhereStr = whereStr;
        p.Orderfld = "ArticleID";
        p.PageSize = this.AspNetPager1.PageSize;
        p.PageIndex = this.AspNetPager1.CurrentPageIndex;
        p.OrderType = "desc";
        p.ControlBind();

        this.AspNetPager1.RecordCount = p.recordcount();

    }

    //绑定文章类型
    public void BindArticleClass()
    {
        DataColumn dc1 = new DataColumn("Classid");
        DataColumn dc2 = new DataColumn("className");
        DataColumn dc3 = new DataColumn("parentId");
        DataColumn dc4 = new DataColumn("ParentPath");
        DataColumn dc5 = new DataColumn("RootID");
        DataColumn dc6 = new DataColumn("Child");
        DataColumn dc7 = new DataColumn("PrevID");
        DataColumn dc8 = new DataColumn("NextID");
        DataColumn dc9 = new DataColumn("OrderID");
        DataColumn dc10 = new DataColumn("Depth");

        data.Columns.Add(dc1);
        data.Columns.Add(dc2);
        data.Columns.Add(dc3);
        data.Columns.Add(dc4);
        data.Columns.Add(dc5);
        data.Columns.Add(dc6);
        data.Columns.Add(dc7);
        data.Columns.Add(dc8);
        data.Columns.Add(dc9);
        data.Columns.Add(dc10);
        ddl_NewsClass_Bind(ddlArticleClass, 0);
    }

    private void initPage()
    {
        if (Request["ArticleID"]!=null)
        {
            ViewState["ArticleID"] = Request["ArticleID"];
        }
        if(Request["Action"]!=null)
        {
            ViewState["Action"] = Request["Action"];
            string Action = Request["Action"];
            switch(Action)
            {
                case "Add":
                    palArticleList.Visible = false;
                    palAddArticle.Visible = true;
                    BindArticleClass();
                    break;
                case "Update":
                    palArticleList.Visible = false;
                    palAddArticle.Visible = true;
                    BindArticleClass();
                    setModel();
                    break;
                case "AllList":
                    break;
                case "MyList":
                    palArticleList.Visible = true;
                    palAddArticle.Visible = false;
                    dataBindMyArticle();
                    break;
            }
        
        }
    }


    //从数据库获取并显示
    private void setModel()
    {
        int ArticleID = 0;
        if (ViewState["ArticleID"] != null)
        {
            ArticleID = Convert.ToInt32(ViewState["ArticleID"].ToString());
        }
        else
        {
            return;
        }
        Article_Model model = articleBLL.getArticleModelByArticleID(ArticleID);
        txtArticleTitle.Text = model.Title;
        txtAuthor.Text = model.Author;
        txtCopyFrom.Text = model.CopyFrom;
        txtKeyWord.Text = model.KeyWord;
        rbtnIsHot.SelectedValue = model.IsHot.ToString();
        rbtnIsTop.SelectedValue = model.IsTop.ToString();
        rbtnIsCommend.SelectedValue = model.IsCommend.ToString();
        if (model.IsPassed == 0)
        {
            chkIsPassed.Checked = false;
        }
        txtArticleContent.Text = model.Content;
        if(model.IncludePic==1)
        {
            chxIsInclude.Checked = true;
        }
        txtHeadImage.Text = model.DefaultPicUrl;
        txtReadScore.Text = model.ReadScore.ToString();
        ddlArticleClass.SelectedValue = model.ClassID.ToString();
        txtTitleFontColor.Text = model.TitleFontColor;
        txtTitleFontType.Text = model.TitleFontType.ToString();
        if(model.TitleFontBlod==1)
        {
            chkTitleFontBlod.Checked = true;
        }
        if(model.TitleFontIta==1)
        {
            chkTitleFontIta.Checked = true;
        }

    }


    //从界面获得文章实体
    private Article_Model getModel()
    {
        Article_Model model = new Article_Model();
        int IsHot = Convert.ToInt32(rbtnIsHot.SelectedValue);
        int IsTop = Convert.ToInt32(rbtnIsTop.SelectedValue);
        int IsCommend = Convert.ToInt32(rbtnIsCommend.SelectedValue);

        
        int ClassId = Convert.ToInt32(ddlArticleClass.SelectedValue);
        int MaxCharPerPage = 0;
        int IsIncludePic = 0;
        int IsPassed = 1;
        int ReadScore = 0;
        int TitleFontType=0;
        int TitleFontBlod = 0;
        int TitleFontIta = 0;
        try
        {
            ReadScore=Convert.ToInt32(txtReadScore.Text);
        }
        catch(Exception ex)
        {
        }
        try
        {
            TitleFontType=Convert.ToInt32(txtTitleFontType.Text);
        }
        catch(Exception ex)
        {
        
        }
        if (!chkIsPassed.Checked)
        {
            IsPassed = 0;
        }
        if(chxIsInclude.Checked)
        {
            IsIncludePic = 1;
        }
        if(chkTitleFontBlod.Checked)
        {
            TitleFontBlod = 1;
        }
        if(chkTitleFontIta.Checked)
        {
            TitleFontIta = 1;
        }
        model.Title = txtArticleTitle.Text;
        model.Author = txtAuthor.Text;
        model.CopyFrom = txtCopyFrom.Text;
        model.IsMaster = 1;
        //管理员编号
        IsExit();
        Admin_Model adminModel = AdminInfo;
        if(adminModel==null)
        {
            Admin_LoginOut();
        }
        else
        {
            model.EditorId = adminModel.Id;
        }
        model.KeyWord = txtKeyWord.Text;
        model.Hits = 0;
        model.UpdateTime = DateTime.Now;
        model.IsHot = IsHot;
        model.IsTop = IsTop;
        model.IsCommend = IsCommend;
        model.IsPassed = IsPassed;
        model.Content = txtArticleContent.Text;
        model.IncludePic = IsIncludePic;
        model.DefaultPicUrl = txtHeadImage.Text;
        model.ReadScore = ReadScore;
        model.ClassID = ClassId;
        model.IsDel = 0;
        model.TitleFontColor = txtTitleFontColor.Text;
        model.TitleFontType = TitleFontType;
        model.TitleFontBlod = TitleFontBlod;
        model.TitleFontIta = TitleFontIta;
        model.MaxCharPerPage = MaxCharPerPage;
        model.AppraiseNum = 0;
        model.ChannelTypeID = 0;
        model.ChannelType = 0;
        return model;
    }

    //发布文章
    private void PublishArticle()
    { 
        Article_Model model=getModel();
        bool isSucce=articleBLL.AddArticle(model);
        if (isSucce)
        {
            ResultStr("Admin_ManageMyArticle.aspx", "文章添加成功");
        }
        else
        {
            ResultStr("Admin_ManageMyArticle.aspx", "文章添加失败");
        }
        
    }

   

    private void ddl_NewsClass_Bind(DropDownList Drop, int ParentID)
    {

        bind_tree(ParentID.ToString());

        string _NewsClass_ID = "-1";
        //dbclass dbc = new dbclass();
        Drop.Items.Add(new ListItem("--请选择--", "-1"));
        //DataTable dt = dbc.GetSql_DataTable("Select * from ArticleClass order by RootID,OrderID") ;

        bool[] arrShowLine = new bool[data.Rows.Count];

        foreach (DataRow dr in data.Rows)
        {
            ArticleClassInfo model = ArticleClass.GetModel(Convert.ToInt32(dr["Classid"].ToString()));

            if (model._NextID > 0)
            {
                arrShowLine[model._Depth] = true;
            }
            else
            {
                arrShowLine[model._Depth] = false;
            }
            string Result = " ";
            if (model._Depth > 0)
            {
                for (int i = 1; i <= model._Depth; i++)
                {
                    if (i == model._Depth)
                    {
                        if (model._NextID > 0)
                        {
                            Result += "├";
                        }
                        else
                        {
                            Result += "└";
                        }
                    }
                    else
                    {
                        if (arrShowLine[i] == true)
                        {
                            Result += "│";
                        }
                        else
                        {
                            Result += "　";
                        }
                    }
                }
            }
            if (model._LinkUrl == "" && model._Child == 0)
            {
                _NewsClass_ID = model._Classid.ToString();
                Result += model._className + "【可选项】";
            }
            else
            {
                _NewsClass_ID = "-1";
                Result += model._className;
            }
            ListItem Item = new ListItem();
            Item.Text = Result;
            Item.Value = _NewsClass_ID;
            Drop.Items.Add(Item);
        }
    }

    private void bind_tree(string ChildNodes)
    {
        //绑定根节点
        dbclass dbc = new dbclass();
        DataTable dt = dbc.GetSql_DataTable("select * from ArticleClass where parentId=" + ChildNodes + "  and Types=1 order by OrderID ");

        foreach (DataRow dr in dt.Rows)
        {
            DataRow dar = data.NewRow();
            dar["Classid"] = dr["Classid"];
            dar["className"] = dr["className"];
            dar["parentId"] = dr["parentId"];
            dar["ParentPath"] = dr["ParentPath"];
            dar["RootID"] = dr["RootID"];
            dar["Child"] = dr["Child"];
            dar["PrevID"] = dr["PrevID"];
            dar["NextID"] = dr["NextID"];
            dar["OrderID"] = dr["OrderID"];
            dar["Depth"] = dr["Depth"];
            data.Rows.Add(dar);

            bind_tree(dr["Classid"].ToString());
        }
    }


    protected void btnAdd_Click(object sender, EventArgs e)
    {
        PublishArticle();
    }
    protected void AspNetPager1_PageChanged(object sender, EventArgs e)
    {

    }


    protected void rptMyArticle1_ItemCommand(object source, RepeaterCommandEventArgs e)
    {

        
        string Action = "";
        if(ViewState["Action"]!=null)
        {
            Action = ViewState["Action"].ToString();
        }
        if (e.CommandName == "deleteArticle")
        {
            LinkButton lbtnDeleteArticle = e.Item.FindControl("lbtnDelete") as LinkButton;
            if(lbtnDeleteArticle!=null)
            {
                string ArticleID = e.CommandArgument.ToString();
                try
                { 
                    articleBLL.DeleteArticle(Convert.ToInt32(ArticleID));
                }
                catch(Exception ex)
                {
                    
                }
            }
        }
        else if (e.CommandName == "setIsTop")
        {
        
        }
        else if (e.CommandName == "setIsCommend")
        {
        
        }
        if(Action!="")
        {
            if(Action=="MyList")
            {
                dataBindMyArticle();
            }
            else if(Action=="AllList")
            {
                
            }
        }

    }
}
